<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/6
 * Time: 13:45
 */

namespace Admin\Controller;


class BbController extends AdminController
{
    public function _myinitialize()
    {
        $this->db = M('bb');
        $this->db1 = M('yw');
        $this->session_lst = CONTROLLER_NAME."lst";
    }

    public function lst()
    {
        $session_where = I('session_where');
        $where['status'] = 1;
        if (empty($session_where)) {
            $xm_id = I('xm_id');
            $arr['xm']=I('xm');
            $arr['yw']=I('yw');
            $arr['zh']=I('zh');
            if (!empty($xm_id)) {
                $where['xm_id'] = $xm_id;
                //将查询条件存入session中，方便更新时跳转
                session(CONTROLLER_NAME . "where", $where);
                session(CONTROLLER_NAME."arr",$arr);
            }
        } else { //不为空，则表明是从add/edit/del中跳转过来的，则查询条件从session中取
            $where = session(CONTROLLER_NAME . "where");
            $arr=session(CONTROLLER_NAME."arr");
            $xm_id=$where['xm_id'];
        }

        C('LIST_ROWS', 200);
        $lists = $this->lists($this->db, $where, "rq desc");
        session($this->session_lst,null);
        session($this->session_lst,$lists);
        $this->assign('lists', $lists);

        $this->assign('xm_id', $xm_id);
        $this->assign('xm',$arr['xm']);
        $this->assign('yw',$arr['yw']);
        $this->assign('zh',$arr['zh']);

        $csv_name=I('csv_name');
        if(!empty($csv_name)){
            $this->assign('csv_name',$csv_name);
        }

        $this->display();
    }

    public function add()
    {
        if (IS_POST) {
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['updatetime'] = time();
                $jg = $this->db1->where(array('xm_id' => $data['xm_id']))->getField('jg');
                if (!$jg) {
                    $jg = 0;//如果在业务表中没有查到此项目的价格，则默认为0
                }
                $data['je'] = $data['sj'] * $jg;
            }
            $rid = $this->db->add($data);

            if ($rid === false) {
                $this->error($this->db->getDbError());
            }
            $this->redirect('lst', array("session_where" => "session_where"));
        }
    }

    public function edit()
    {
        if (IS_POST) {
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['updatetime'] = time();
                $jg = $this->db1->where(array('xm_id' => $data['xm_id']))->getField('jg');
                if (!$jg) {
                    $jg = 0;//如果在业务表中没有查到此项目的价格，则默认为0
                }
                $data['je'] = $data['sj'] * $jg;

                $rid = $this->db->save($data);

                if ($rid === false) {
                    $this->error($this->db->getDbError());
                }
                $this->redirect('lst', array("session_where" => "session_where"));
            }
        } else {
            $id = I('id');
            if (!empty($id)) {
                $data = $this->db->where(array("id" => $id))->find();
                $this->ajaxReturn($data);
            }
        }
    }

    public function del()
    {
        $id = I('id');
        if (!empty($id)) {
            $this->db->where(array('id' => $id))->delete();
            $this->redirect('lst', array("session_where" => "session_where"));
        }
    }

    public function csv_export(){
        $this->export();
    }
}